home *** CD-ROM | disk | FTP | other *** search
- Path: engnews1.Eng.Sun.COM!taumet!clamage
- From: Nico Josuttis <nico@bredex.de>
- Newsgroups: comp.std.c++
- Subject: string::npos is unhandy and dangerous
- Date: 2 Apr 1996 15:16:07 GMT
- Organization: ?
- Approved: clamage@eng.sun.com (comp.std.c++)
- Message-ID: <199604020902.LAA06787@bredex.bredex.de>
- NNTP-Posting-Host: taumet.eng.sun.com
- X-Authentication-Warning: bredex.bredex.de: Host localhost didn't use HELO protocol
- X-Mts: smtp
- Content-Length: 1196
- Originator: clamage@taumet
-
-
- Following the standard, all find() member functions for string
- return string::size_type and may have the value string::npos,
- which is (size_type)-1.
-
- I have some problems with that:
- 1.) If i understand it right I have to write:
-
- string::size_type pos;
-
- pos = s.find('x');
- if (pos == string::npos)
- // not found
- ...
-
- This seems unhandy und thus dangerous as people might
- try to make the code simpler:
- For example:
- int pos
- pos = s.find('x');
- if (pos == string::npos)
- or even
- if (pos == -1)
-
- The problem is that the test with operator== fails if
- sizeof(size_type) != sizeof(int)
- because unfortunately then
- (size_type)-1 is not -1
- This is due to the conversion to unsigned.
-
- Am I missing something or are these problems known?
- Why wasn't taken a simpler and safer interface?
-
- 2.) I wonder what "npos" means.
- Could somebody tell me?
-
- --------
- Nico address: BREDEX GmbH
- email: nico@bredex.de Nicolai Josuttis
- Fallersleber-Tor-Wall 23
- phone: +49 531 24330-0 D-38100 Braunschweig
- fax: +49 531 24330-99 Germany
- --------
-
-
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-